/**
 * COPYRIGHT© KMTC 2007
 * This software is the proprietary information of Korea Marine Transport Co.LTD
 * 
 * Revision History
 * Author			Date		Description
 * ------			----		-----------
 * esther			08.01.10	
 */

package kr.or.javacafe.common.mail;

import java.util.Map;
import org.springframework.mail.javamail.JavaMailSender;

import org.apache.commons.logging.*;

public class MailManagerImpl implements MailManager {
	protected final static Log logger = LogFactory.getLog(MailManagerImpl.class);
	
//	private final String SAIL_SCHEDULE = "SAIL_SCHEDULE";

	private JavaMailSender sender;
	private HtmlMimeMessagePreparator preparator;

	public void setSender(JavaMailSender sender) {
		this.sender = sender;
	}

	public void setPreparator(HtmlMimeMessagePreparator preparator) {
		this.preparator = preparator;
	}

	public void sendMail(String templateName, Map data) {
		preparator.setHtmlTemplate("/jsp/mail/sailSchedule.jsp");
		preparator.setData(data);
		logger.debug("start mail send");
		try{
			sender.send(preparator);
		}catch (Exception mex) {
			logger.debug("Exception mex"+mex);
			mex.printStackTrace(); 
		}
		logger.debug("end mail send");
	}

	public void sendMail(String from, String to, String subject,
			String templateName, Map data) {
		preparator.setFrom(from);
		preparator.setTo(to);
		preparator.setSubject(subject);
		sendMail(templateName, data);
	}

	public void sendMail(String fieldValue, String templateName,
			Map data, String fieldName) {
		if ("from".equals(fieldName)) {
			preparator.setFrom(fieldValue);
		} else if ("to".equals(fieldName)) {
			preparator.setTo(fieldValue);
		} else if ("subject".equals(fieldValue)) {
			preparator.setSubject(fieldValue);
		}
		sendMail(templateName, data);
	}

	public void sendMail(String from, String to, String templateName, Map data) {
		preparator.setFrom(from);
		preparator.setTo(to);
		sendMail(templateName, data);
	}

}

